/*
 * NotesPlugin.java
 * 
 * Copyright (c) 2007
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.dcarew.notes;

import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.ui.plugin.AbstractUIPlugin;
import org.osgi.framework.BundleContext;

/**
 * The activator class controls the plug-in life cycle.
 * 
 * @author Devon Carew
 */
public class NotesPlugin
	extends AbstractUIPlugin
{
	public static final String	PLUGIN_ID	= "org.dcarew.notes";

	private static NotesPlugin	plugin;
	
	
	/**
	 * The constructor.
	 */
	public NotesPlugin()
	{
		
	}

	public void start(BundleContext context)
		throws Exception
	{
		super.start(context);
		
		plugin = this;
	}
	
	public void stop(BundleContext context)
		throws Exception
	{
		plugin = null;
		
		super.stop(context);
	}
	
	/**
	 * Returns the shared instance.
	 * 
	 * @return the shared instance
	 */
	public static NotesPlugin getDefault()
	{
		return plugin;
	}
	
	/**
	 * Returns an image descriptor for the image file at the given plug-in relative path.
	 * 
	 * @param path the path
	 * @return the image descriptor
	 */
	public static ImageDescriptor getImageDescriptor(String path)
	{
		return imageDescriptorFromPlugin(PLUGIN_ID, path);
	}

	// /////////////////////////////////////////////////////////////////////////////
	//
	// Logging
	//
	// /////////////////////////////////////////////////////////////////////////////

	/**
	 * Logs a warning with this plug-in's log.
	 * 
	 * @param message the warning message
	 */
	public static void logWarning(String message)
	{
		Status errorStatus = new Status(IStatus.WARNING, PLUGIN_ID, IStatus.OK, message, null);
		log(errorStatus);
	}
	
	/**
	 * Logs an error with this plug-in's log.
	 * 
	 * @param message the error message
	 */
	public static void logError(String message)
	{
		log(new Status(IStatus.ERROR, PLUGIN_ID, IStatus.OK, message, null));
	}
	
	/**
	 * Logs the specified status with this plug-in's log.
	 * 
	 * @param status status to log
	 */
	public static void log(IStatus status)
	{
		getDefault().getLog().log(status);
	}
	
	/**
	 * Logs the specified throwable with this plug-in's log.
	 * 
	 * @param t throwable to log
	 */
	public static void log(Throwable t)
	{
		log(newErrorStatus("Notes Plugin", t));
	}
	
	/**
	 * Returns a new error status for this plug-in with the given message
	 * 
	 * @param message the message to be included in the status
	 * @param exception the exception to be included in the status or <code>null</code> if none
	 * @return a new error status
	 */
	public static IStatus newErrorStatus(String message, Throwable exception)
	{
		return new Status(IStatus.ERROR, PLUGIN_ID, message, exception);
	}

}
